package com.my.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.my.demo.config.ZhenZi;
import com.my.demo.pojo.Result;
import com.my.demo.pojo.User;
import com.my.demo.service.UserService;

import com.my.demo.utils.SaltUtils;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
//用户进行注册的Controller
public class RegisterController {
  @Autowired
  UserService userService;
  @Autowired
  ZhenZi zhenZi;

  //发送短信的的接口
  @PostMapping("/sendSms")
  @CrossOrigin
  public Result sendSms(@RequestBody HashMap<String, String> map, HttpSession session) {
    Result result = new Result();
    result.setSuccess(false);
    result.setDetail(null);

    String phone = map.get("userName");
    JSONObject user_json = (JSONObject) session.getAttribute(phone);

    if (user_json != null && (System.currentTimeMillis() - (Long) user_json.get("createTime") < 1000 * 60 * 1)) {
      result.setMsg("验证码1分钟只能请求一次");
    } else {
      try {
        JSONObject json = zhenZi.send_message(phone);
        long createTime = System.currentTimeMillis();
        json.put("createTime", createTime);
        session.setAttribute(phone, json);

        if (json.getString("code").equals("0")) {
          result.setMsg("发送成功");
          result.setSuccess(true);
        } else {
          result.setMsg("发送失败");
        }

        result.setDetail(json);
      } catch (Exception e) {
        result.setMsg(e.getMessage());
        e.printStackTrace();
      }
    }
    return result;
  }

  //进行验证码的校验后，提供注册的接口
  @PostMapping("/register")
  @CrossOrigin
  public Result toRegister(HttpSession session, @RequestBody Map<String, String> models) {
    Result result = new Result();
    result.setSuccess(false);
    result.setDetail(null);

    User user = new User(models.get("userName"), models.get("password"), models.get("userType"));
    String passWd2 = models.get("passWd2");
    String verifyCode = models.get("verifyCode");

    JSONObject user_json = (JSONObject) session.getAttribute(user.getUserName());
    if (user_json == null) {
      result.setMsg("未发送验证码");
      return result;
    }

    QueryWrapper<User> wrapper = new QueryWrapper<>();
    wrapper.eq("user_name", user.getUserName());
    try {
      User existUser = userService.getOne(wrapper);
      if (existUser != null) {
        //如果用户名已存在
        result.setMsg("用户名已存在");
      } else if (!passWd2.equals(user.getPassword())) {
        result.setMsg("两次密码不一致");
      } else if (!user_json.getString("verifyCode").equals(verifyCode)) {
        result.setMsg("验证码错误");
      } else if ((System.currentTimeMillis() - (Long) user_json.get("createTime")) > 1000 * 60 * 5) {
        result.setMsg("验证码已经过期");
      } else {


        //1.生成随机盐
        String salt = SaltUtils.getSalt(6);
        //2.将随机盐保存到数据
        user.setSalt(salt);
        //3.明文密码进行md5 + salt + hash散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        user.setPassword(md5Hash.toHex());


        userService.save(user);
        result.setSuccess(true);
        result.setMsg("注册成功");

        //注册成功后，移除验证码信息
        session.removeAttribute(user.getUserName());
        result.setDetail(user);
      }
    } catch (Exception e) {
      result.setMsg(e.getMessage());
      e.printStackTrace();
    }
    return result;
  }
//  @PostMapping("/bussiness_register")
//  public Result toRegister(HttpSession session, @RequestBody Map<String, String> models) {
//    Result result=new Result();
//    result
//  }
}
